home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_10 / a10_1.m next >
Encoding:
Text File  |  1994-06-05  |  2.0 KB  |  86 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 10.1 (Finite-Difference Solution for the Wave Equation).
  9. % Section    10.1,    Hyperbolic Equations, Page 507
  10. echo on; clc; format short; hold off; clear
  11.  
  12. %  HYPERBOLIC PDE's.
  13. %  Finite difference solution for the wave equation
  14.  
  15. %                         2
  16. %          u  (x,t)   =  c  u  (x,t)     with
  17. %           tt               xx
  18.  
  19. %   u(0,t) = 0     and   u(a,t) = 0      for 0 ≤ t ≤ b.
  20.  
  21. %   u(x,0) = f(x)  and  u (x,0) = g(x)   for  0 < x < a.
  22. %                        t
  23.    
  24. % A numerical approximation is computed over the rectangle
  25.  
  26. %         0 ≤ x ≤ a ,   0 ≤ t ≤ b.
  27.  
  28. pause    % Press any key to continue.
  29.  
  30. clc;
  31. %    Store f(x) and g(x) in the M-files f.m and g.m respectively.
  32. % function z = f(x)
  33. % z = sin(pi*x) + sin(2*pi*x);
  34.  
  35. % function z = g(x)
  36. % z = 0;
  37.  
  38. delete f.m
  39. diary f.m; disp('function z = f(x)');...
  40.            disp('z = sin(pi*x) + sin(2*pi*x);');...
  41. diary off;
  42.  
  43. delete g.m
  44. diary g.m; disp('function z = g(x)');...
  45.            disp('z = 0;');...
  46. diary off;
  47.  
  48. % Remark. f.m  g.m finedif.m are used for Algorithm 10.1
  49. f(0); g(0);
  50. pause    % Press any key to continue.
  51.  
  52. clc;
  53. %    Place the endpoint of [0,a] in  a.
  54.  
  55. %    Place the endpoint of [0,b] in  b.
  56.  
  57. %    For the wave equation, enter the constant  c.
  58.  
  59. %    Over [0,a] enter the number of grid points  n.
  60.  
  61. %    Over [0,b] enter the number of grid points  m.
  62.  
  63. a  =  1;
  64. b  =  0.5;
  65. c  =  2;
  66. n  =  11;
  67. m  =  11;
  68.  
  69. U = finedif('f','g',a,b,c,n,m);
  70.  
  71. pause    % Press any key to see the solution. 
  72.  
  73. clc; clg;
  74. Z = fliplr(U);
  75. mesh(Z);...
  76. Mx1='The finite difference solution to the wave equation.';...
  77. title(Mx1);...
  78. shg; pause % Press any key to continue.
  79.  
  80. W = U';
  81. points = W(:,2:n-1);
  82. clc,echo off,diary output,...
  83. disp(' '),disp(Mx1),disp(' '),disp(points),...
  84. diary off,echo on
  85.  
  86.